//
//  LHCountryHomePageCollectionView.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/14.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHCountryHomePageCollectionView.h"
#import "UIImageView+WebCache.h"
#import "LHRotatePicturesModel.h"
#include "UIImageView+WebCache.h"

@interface LHCountryHomePageCollectionView ()<UIScrollViewDelegate>

@end

@implementation LHCountryHomePageCollectionView

- (void)dealloc
{
    [_rotateModelArr release];
    [_titlePicScrollView release];
    [super dealloc];
}

/**
 *  重写初始化方法
 *
 *  @param frame self框架大小
 *
 *  @return self
 */
- (instancetype)initWithFrame:(CGRect)frame picModelArr:(NSArray *)picModelArr
{
    if (self = [super initWithFrame:frame]) {
        
        [self createScrollViewWithPicModelArr:picModelArr];
    }
    return self;
}
/**
 *  创建轮播图
 */
- (void)createScrollViewWithPicModelArr:(NSArray *)picModelArr
{
   
    self.titlePicScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _titlePicScrollView.backgroundColor = [UIColor whiteColor];
    _titlePicScrollView.contentSize = CGSizeMake(self.frame.size.width * 6, 0);
    [_titlePicScrollView setContentOffset:CGPointMake(LHWidth, 0)];
    _titlePicScrollView.pagingEnabled = YES;
    _titlePicScrollView.bounces = NO;
    _titlePicScrollView.showsVerticalScrollIndicator = NO;
    _titlePicScrollView.showsHorizontalScrollIndicator = NO;
    
    //签订协议
    self.titlePicScrollView.delegate = self;
   
    //添加
    [self addSubview:self.titlePicScrollView];
    [_titlePicScrollView release];

    UIImageView *viewFirst = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, LHWidth, self.frame.size.height)];
    [viewFirst sd_setImageWithURL:[NSURL URLWithString:[[picModelArr lastObject]img]]];
    [self.titlePicScrollView addSubview:viewFirst];
    [viewFirst release];
    
    UIImageView *viewLast = [[UIImageView alloc]initWithFrame:CGRectMake(LHWidth * 5, 0, LHWidth, self.frame.size.height)];
    [viewLast sd_setImageWithURL:[NSURL URLWithString:[[picModelArr firstObject]img]]];
    [self.titlePicScrollView addSubview:viewLast];
    [viewLast release];
    
    [self creatViewWithPicModelArr:picModelArr];
    
}
- (void)creatViewWithPicModelArr:(NSArray *)picModelArr
{
    for (NSInteger i = 1; i < picModelArr.count + 1; i++) {
        UIImageView *view2 = [[UIImageView alloc] initWithFrame:CGRectMake( LHWidth * i, 0, LHWidth, self.frame.size.height )];
        [view2 sd_setImageWithURL:[NSURL URLWithString:[[picModelArr objectAtIndex:i - 1]img]]];
        [self.titlePicScrollView addSubview:view2];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.titlePicScrollView.contentOffset.x == 0) {
        self.titlePicScrollView.contentOffset = CGPointMake(self.frame.size.width * 4, 0);
    } else if (self.titlePicScrollView.contentOffset.x == self.frame.size.width * 5){
        self.titlePicScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
}


@end
